package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
* @author 31439
* @description 针对表【tb_type(商品分类表)】的数据库操作Service实现
* @createDate 2025-07-21 14:18:45
*/
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
    implements TbTypeService{
    @Autowired
    TbTypeMapper tbTypeMapper;
    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Override
    public R getTypeTree(PageInfoVo pageInfoVo) {
        QueryWrapper<TbType> wrapper = new QueryWrapper<>();
        //判断是否需要进行模糊查询或精确查询
        if(pageInfoVo.getKeyWord() != null && pageInfoVo.getKeyWord() != ""){
            wrapper.lambda().like(TbType::getTypeName,pageInfoVo.getKeyWord());
        }
        if(pageInfoVo.getTypeStatus() != null){
            wrapper.lambda().eq(TbType::getTypeStatus,pageInfoVo.getTypeStatus());
        }
        if(pageInfoVo.getKeyWord() != null && pageInfoVo.getKeyWord()!= "" || pageInfoVo.getTypeStatus() != null){
            //查询分类
            List<TbType> list = tbTypeMapper.selectList(wrapper);
            return R.OK(list);
        }

        //查询树形结构
        List<TbType> listAll = tbTypeMapper.selectList(null);

        //构造树形结构
        List<Tree<String>> treeList = TreeUtil.build(listAll, "0", (type, tree) -> {
            tree.setId(type.getTypeId().toString());
            tree.setName(type.getTypeName());
            tree.setParentId(type.getTypePid().toString());

            tree.putExtra("typeStatus", type.getTypeStatus());
            tree.putExtra("typeImage", type.getTypeImage());
            tree.putExtra("typeTop", type.getTypeTop());
            tree.putExtra("createTime",type.getCreateTime());
            tree.putExtra("typeId",type.getTypeId());
            tree.putExtra("typeName",type.getTypeName());
        });
        return R.OK(treeList);
    }

    @Override
    public R uploadImages(MultipartFile file) {

        String extName = FileNameUtil.extName(file.getOriginalFilename());
        String url = null;
        try {
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            if(storePath == null){
                return R.ERROR(500,"上传失败");
            }
            url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"上传失败");
        }


    }

    @Override
    public R addType(TbType tbType) {
        //检查唯一性
        TbType type = tbTypeMapper.selectOne(new QueryWrapper<TbType>().lambda().eq(TbType::getTypeName, tbType.getTypeName()));
        if(type != null){
            return R.ERROR(500,"名称已存在");
        }
        tbTypeMapper.insert(tbType);

        return R.OK();
    }

    @Override
    public R getSelectTypeTree() {
        //查询所有分类
        List<TbType> listAll = tbTypeMapper.selectList(null);

        //构造树形结构
        List<Tree<String>> treeList = TreeUtil.build(listAll, "0", (type, node) -> {
            node.setId(type.getTypeId().toString());
            node.setParentId(type.getTypePid().toString());
            node.setName(type.getTypeName());

            node.putExtra("label",type.getTypeName());
            node.putExtra("value",type.getTypeId().toString());


        });
        return R.OK(treeList);
    }
}




